package com.ruoyi.project.WebMoudle.onlineCoupon.controller;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.text.Convert;
import com.ruoyi.project.WebMoudle.WxUser.domain.WxUser;
import com.ruoyi.project.WebMoudle.WxUser.service.IWxUserService;
import com.ruoyi.project.WebMoudle.vipCoupon.domain.VipCoupon;
import com.ruoyi.project.WebMoudle.vipCoupon.service.IVipCouponService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.project.WebMoudle.onlineCoupon.domain.OnlineCoupon;
import com.ruoyi.project.WebMoudle.onlineCoupon.service.IOnlineCouponService;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.web.page.TableDataInfo;

/**
 * 优惠券Controller
 *
 * @author ruoyi
 * @date 2020-07-09
 */
@Controller
@RequestMapping("/onlineCoupon/onlineCoupon")
public class OnlineCouponController extends BaseController {
    private String prefix = "onlineCoupon/onlineCoupon";

    @Autowired
    private IOnlineCouponService onlineCouponService;
    @Autowired
    private IVipCouponService vipCouponService;
    @Autowired
    private IWxUserService wxUserService;

    @RequiresPermissions("onlineCoupon:onlineCoupon:view")
    @GetMapping()
    public String onlineCoupon() {
        return prefix + "/onlineCoupon";
    }

    @RequiresPermissions("onlineCoupon:onlineCoupon:view")
    @GetMapping("/selectVip/{couponId}")
    public String selectVip(@PathVariable("couponId") Long couponId,ModelMap mmap) {

        mmap.put("couponId",couponId);
        return prefix + "/selectVip";
    }

    /**
     * 查询优惠券列表
     */
    @RequiresPermissions("onlineCoupon:onlineCoupon:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(OnlineCoupon onlineCoupon) {
       //onlineCouponService.updateExpire();
        startPage();
        List<OnlineCoupon> list = onlineCouponService.selectOnlineCouponList(onlineCoupon);
        return getDataTable(list);
    }

    /**
     * 导出优惠券列表
     */
    @RequiresPermissions("onlineCoupon:onlineCoupon:export")
    @Log(title = "优惠券", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(OnlineCoupon onlineCoupon) {
        List<OnlineCoupon> list = onlineCouponService.selectOnlineCouponList(onlineCoupon);
        ExcelUtil<OnlineCoupon> util = new ExcelUtil<OnlineCoupon>(OnlineCoupon.class);
        return util.exportExcel(list, "onlineCoupon");
    }

    /**
     * 新增优惠券
     */
    @GetMapping("/add")
    public String add() {
        return prefix + "/add";
    }

    /**
     * 新增保存优惠券
     */
    @RequiresPermissions("onlineCoupon:onlineCoupon:add")
    @Log(title = "优惠券", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(OnlineCoupon onlineCoupon) {
        return toAjax(onlineCouponService.insertOnlineCoupon(onlineCoupon));
    }

    /**
     * 修改优惠券
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap) {
        OnlineCoupon onlineCoupon = onlineCouponService.selectOnlineCouponById(id);
        mmap.put("onlineCoupon", onlineCoupon);
        return prefix + "/edit";
    }

    /**
     * 修改保存优惠券
     */
    @RequiresPermissions("onlineCoupon:onlineCoupon:edit")
    @Log(title = "优惠券", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(OnlineCoupon onlineCoupon) {
        return toAjax(onlineCouponService.updateOnlineCoupon(onlineCoupon));
    }

    /**
     * 删除优惠券
     */
    @RequiresPermissions("onlineCoupon:onlineCoupon:remove")
    @Log(title = "优惠券", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        return toAjax(onlineCouponService.deleteOnlineCouponByIds(ids));
    }

    /**
     * 下发优惠券
     */
    @RequiresPermissions("onlineCoupon:onlineCoupon:edit")
    @Log(title = "优惠券", businessType = BusinessType.UPDATE)
    @PostMapping("/sendCouponToVip")
    @ResponseBody
    @Transactional
    public AjaxResult sendCouponToVip(Long  couponId,String wids) {
        if (!StringUtils.isNotEmpty(wids)){
            return error("请勾选下发对象");
        }
        String[] ids = Convert.toStrArray(wids);

        ArrayList<VipCoupon> list = new ArrayList<>();
        for (String wid : ids) {     //创建会员优惠券表
            VipCoupon vipCoupon = new VipCoupon();
            vipCoupon.setCouponId(couponId);
            vipCoupon.setVipId(Long.parseLong(wid));
            vipCoupon.setCreateTime(new Date());
            list.add(vipCoupon);
        }
        vipCouponService.addSomeEnety(list);
        return toAjax(onlineCouponService.changeStatus(couponId,"1"));
    }


    /**
     * 下发给所有人优惠券
     */
    @RequiresPermissions("onlineCoupon:onlineCoupon:edit")
    @Log(title = "优惠券", businessType = BusinessType.UPDATE)
    @PostMapping("/toSendAll")
    @ResponseBody
    @Transactional
    public AjaxResult toSendAll(WxUser wxUser,Long couponId) {
        List<WxUser> userList = wxUserService.selectWxUserList(wxUser);
        ArrayList<VipCoupon> list = new ArrayList<>();
        if (userList.size()>0){
            for (WxUser user : userList) {
                VipCoupon vipCoupon = new VipCoupon();
                vipCoupon.setCouponId(couponId);
                vipCoupon.setVipId(user.getId());
                vipCoupon.setCreateTime(new Date());
                list.add(vipCoupon);
            }
        }else {
            return error("无符合下放优惠券对象");
        }
        vipCouponService.addSomeEnety(list);
        return toAjax(onlineCouponService.changeStatus(couponId,"1"));
    }

}
